package com.assistant.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.assistant.R;
import com.assistant.adapter.ListAdpter;
import com.assistant.view.CustomListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity  implements CustomListView.LoadLisener {
    com.assistant.view.CustomListView listView;
    ListAdpter loanAdpter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_list = (Button) findViewById(R.id.bt_list);

        btn_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoanListActivity.class);
                startActivity(intent);
            }
        });
        final LinearLayout message_list = (LinearLayout)findViewById(R.id.message_list);
        final LinearLayout contact_list = (LinearLayout)findViewById(R.id.contact_list);
        LinearLayout message = (LinearLayout)findViewById(R.id.message);
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TalkActivity.class);
                startActivity(intent);
                //message_list.setVisibility(View.VISIBLE);
                //contact_list.setVisibility(View.GONE);

            }
        });
        LinearLayout contact = (LinearLayout)findViewById(R.id.contact);
        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message_list.setVisibility(View.GONE);
                contact_list.setVisibility(View.VISIBLE);
                listView = (CustomListView) findViewById(R.id.loan_list);
                //listView.setOnScrollListener();
                Log.i("TAG", String.valueOf(listView.getLastVisiblePosition()));
                if(listView.getLastVisiblePosition()<=0){
                    showListView(getData(5));
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private List<Map<String,Object>> getData(int num){
        List<Map<String,Object>> listItems = new ArrayList<>();
        for (int i =0; i<num; i++ ) {
            Map<String,Object> listItem = new HashMap<>();
            listItem.put("user_logo","http://img4.duitang.com/uploads/item/201502/15/20150215224242_Z5mBC.thumb.224_0.jpeg");
            listItem.put("user_name","测试"+i);
            listItem.put("send_time","09:54");
            listItem.put("msg","你好"+i);
            listItems.add(listItem);
        }
        return listItems;
    }

    @Override
    public void onLoad() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                showListView(getData(5));
                listView.loadComplete();
            }
        }, 2000);
    }



    void showListView(List<Map<String,Object>> list) {
        if (loanAdpter == null) {
            listView = (CustomListView) findViewById(R.id.loan_list);
            listView.setLoadLisener(this);
            loanAdpter = new ListAdpter(this,list);
            listView.setAdapter(loanAdpter);
        } else {
            loanAdpter.onDataChange(list);
        }
    }
}
